<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'is_extend', 'group_id', 'open_day', 'open_hour', 'image', 'order_type', 'time_span'];

    public function group(){
        return $this->belongsTo(Group::class);
    }

    public function orders(){
        return $this->belongsToMany(Order::class,'order_rooms','room_id','order_id');
    }

    /**
     * @param $data
     * @param null $id
     */
    public static function dataSave($data, $id = null ): Room
    {
        if($id){
            unset($data['created_at']);
            unset($data['updated_at']);
            self::query()->where('id',$id)->update($data);
            $room = self::query()->find($id);
        }else{
            $room = self::create($data);
        }
        return $room;
    }
}
